def header() -> str:
    return 'from transitions import Machine\r\n\r\n'


def states_declaration(states) -> str:
    ret = ''
    for s in states:
        ret += f"S_{s} = '{s}'\r\n"

    return ret


def event_declaration(events) -> str:
    ret = ''
    for e in events:
        ret += f"E_{e} = '{e}'\r\n"

    return ret


def class_declaration(classname, states) -> str:
    ret = ''

    ret += f'class {classname}(object):\r\n'
    states_str = ', '.join([f'S_{s}' for s in states])
    states_repr = '    states = [' + states_str + ']\r\n'

    ret += states_repr

    return ret


def repr_of_a_transition(s_from, s_to, trigger):
    '''
    {'trigger': EVENT_SELECTING_CARD, 'source': STATUS_IDLE, 'dest': STATUS_SELECTING,
             'after': [self._select_card, self._start_select_timer]},
    '''
    from_state = f'S_{s_from}'
    to_state = f'S_{s_to}'
    trigger_repr = f'E_{trigger}'
    method_name = f'from_{s_from}_to_{s_to}_by_{trigger}'

    ret = f"'trigger': {trigger_repr}, 'source': {from_state}, 'dest': {to_state}, 'after': [self.{method_name}]"

    ret = '{' + ret + '}'

    return ret


def transitions_repr(machine, classname) -> str:
    ret = '    def __init__(self):\r\n'

    ret += ' ' * 8 + 'trans = [\r\n' + ' ' * 12

    transitions_str = ',\r\n            '.join(
        [repr_of_a_transition(t.from_state, t.to_state, t.trigger) for t in machine.transitions]
    )

    ret += transitions_str

    ret += '\r\n' + ' ' * 8 + ']\r\n'

    ret += (' ' * 8 +  f'self.machine = Machine(mode=self, states = {classname}.states, transitions=trans, ignore_invalid_triggers=True)\r\n\r\n')

    return ret


def methods_repr(methods) -> str:
    ret = ''

    for method in methods:
        ret += (' ' * 4 + f'def {method}(self):\r\n' + ' ' * 8 + 'pass\r\n\r\n')

    return ret